/** DEMOKS 配网库
 *  
 */

#include "include.h"

/* The examples use WiFi configuration that you can set via project configuration menu.

   If you'd rather not, just change the below entries to strings with
   the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_ESP_WIFI_SSID "lononLED"
#define EXAMPLE_ESP_WIFI_PASS "\0"
#define EXAMPLE_MAX_STA_CONN 1

static const char *TAG = "demoks_access_net";

xQueueHandle demoks_access_net_event;

static void wifi_event_handler(void *arg, esp_event_base_t event_base,
                               int32_t event_id, void *event_data)
{
    if (event_id == WIFI_EVENT_AP_STACONNECTED)
    {
        wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
        ESP_LOGI(TAG, "station " MACSTR " join, AID=%d",
                 MAC2STR(event->mac), event->aid);
    }
    else if (event_id == WIFI_EVENT_AP_STADISCONNECTED)
    {
        wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
        ESP_LOGI(TAG, "station " MACSTR " leave, AID=%d",
                 MAC2STR(event->mac), event->aid);
    }
}

void demoks_sta_mode(bool on_start)
{
    /**
     * 读取WIFI的SSID和密码进行连接
     */
    char ssid[32] = {0};
    char passwd[32] = {0};
    wifi_config_t wifi_config = {0};
    demoks_system_get_wifi_ssid_passwd(&ssid, &passwd);
    if (strlen(ssid) > 0)
    {
        strncpy((char *)&wifi_config.sta.ssid, ssid, 32);
        strncpy((char *)&wifi_config.sta.password, passwd, 32);
        ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
        if (on_start)
        {
            ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
            ESP_ERROR_CHECK(esp_wifi_start());
        }
        ESP_ERROR_CHECK(esp_wifi_connect());
    }
}

void wifi_init_softap()
{
    tcpip_adapter_init();
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));

    wifi_config_t wifi_config = {
        .ap = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
            .password = EXAMPLE_ESP_WIFI_PASS,
            .max_connection = EXAMPLE_MAX_STA_CONN,
            .authmode = WIFI_AUTH_WPA_WPA2_PSK},
    };
    if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0)
    {
        wifi_config.ap.authmode = WIFI_AUTH_OPEN;
    }

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); // Set the WiFi API configuration storage type
    ESP_ERROR_CHECK(esp_wifi_start());

    ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s",
             EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);

    demoks_sta_mode(false);
}

/**
 * 初始化配网模式
 * AP + HTTP
 */
void demoks_access_net_ap_http_init()
{
    demoks_access_net_event = xQueueCreate(1, sizeof(demoks_access_net_event_code_t));
    // 初始化并开启AP模式
    wifi_init_softap();
    // 开启http服务器
    ap_http_server_run();
}

void demoks_access_net_stop()
{
    int a = esp_wifi_stop();
    if (a != ESP_OK)
    {
        ESP_LOGE(TAG, "暂停配网模式失败");
    }
    else
        demoks_sta_mode(true);
}
